package cn.topcodes.tcsf.admin.web.controller.impl;

import cn.topcodes.tcsf.admin.commons.api.security.ShiroUtil;
import cn.topcodes.tcsf.admin.domain.entity.SysUser;
import cn.topcodes.tcsf.admin.domain.form.BaseSettings;
import cn.topcodes.tcsf.admin.service.SystemSettingsService;
import cn.topcodes.tcsf.admin.service.SystemPropertyService;
import cn.topcodes.tcsf.admin.service.org.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
@RequestMapping("/admin")
public class IndexController {

	@Autowired
	private SystemSettingsService systemSettingsService;

	@Autowired
	private SystemPropertyService propService;

	@Autowired
	private SysUserService sysUserService;

	/*管理后台主页*/
	@RequestMapping(value="index",method=RequestMethod.GET)
	public String index(Model model) {
		String name = ShiroUtil.getName();
		model.addAttribute("name",name);
		Long userId = ShiroUtil.getUserId();
		model.addAttribute("userId",userId);
		BaseSettings baseSettings = this.systemSettingsService.getBaseSettings();
		model.addAttribute("baseSettings",baseSettings);
		model.addAttribute("currentUser", ShiroUtil.getUser());
		String homePage = propService.findSysProperty(SystemPropertyService.HOME_PAGE,String.class,"admin/home");
		model.addAttribute("homePage",homePage);
		return "admin/index";
	}
	
	/*欢迎页面*/
	@RequestMapping(value="welcome",method=RequestMethod.GET)
	public String welcome(Model model) {
		return "admin/welcome";
	}
	
	/*管理后台登录页*/
	@RequestMapping(value="login",method=RequestMethod.GET)
	public String login(Model model) {
		Subject subject = SecurityUtils.getSubject();
		if(subject.isAuthenticated()) {
			return "redirect:/admin/index";
		}
		BaseSettings baseSettings = this.systemSettingsService.getBaseSettings();
		model.addAttribute("baseSettings",baseSettings);
		return "admin/login";
	}
	
	/*修改密码页面*/
	@RequestMapping(value="modify-password",method=RequestMethod.GET)
	public String modifyPassword() {
		return "admin/personal/modify-password";
	}

	/*修改个人信息页面*/
	@RequestMapping(value="modify-userinfo",method=RequestMethod.GET)
	public String modifyUserInfo(Model model) {
		SysUser sysUser = this.sysUserService.findOne(ShiroUtil.getUserId());
		model.addAttribute("sysUser", sysUser);
		return "admin/personal/modify-userinfo";
	}
	
	/*功能正在开发中*/
	@RequestMapping(value="coding",method=RequestMethod.GET)
	public String coding() {
		return "admin/coding";
	}

	@RequestMapping(value = "sys-upgrade",method = RequestMethod.GET)
	public String about() {
		return "admin/sys-upgrade";
	}

}
